VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsAutoNumber"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit


Public Function Generate_CtrlNo2(sMenuName As String, Optional ByVal sDate As String) As String
'Dim oRecordset As New clsRecordset
Dim oForm As New clsFormSetup
Dim sFormat As String, sNumerals As String, sAlphabet As String, sReset As String, sDateVal As String
Dim nNum As Integer, sCtrlNo As String
On Error Resume Next
    sFormat = GetValueFrTable("cNumberFormat", "system_autonumber", "cCode ='" & sMenuName & "'")
    sReset = GetValueFrTable("cResetCtr", "system_autonumber", "cCode ='" & sMenuName & "'")
    sAlphabet = GetValueFrTable("cAlphaVal", "system_autonumber", "cCode ='" & sMenuName & "'")
    nNum = GetValueFrTable("nCtr", "system_autonumber", "cCode ='" & sMenuName & "'")
    sDateVal = GetValueFrTable("cDate", "system_autonumber", "cCode ='" & sMenuName & "'")
    If sDateVal = "" Then sDateVal = sDate
    Select Case sReset
        Case ""
            nNum = nNum + 1
        Case "NONE"
            nNum = nNum + 1
        Case "DAILY"
            If Format(sDate, "MMDDYYYY") = Format(sDateVal, "MMDDYYYY") Then
                nNum = nNum + 1
            Else
                nNum = 1
            End If
        Case "MONTHLY"
            If Format(sDate, "MMYYYY") = Format(sDateVal, "MMYYYY") Then
                nNum = nNum + 1
            Else
                nNum = 1
            End If
        Case "ANNUALY"
            If Format(sDate, "YYYY") = Format(sDateVal, "YYYY") Then
                nNum = nNum + 1
            Else
                nNum = 1
            End If
    End Select

    sCtrlNo = Replace(sFormat, GetStringVal(sFormat, "#"), Format(nNum, FormatNumeric(nNum, Len(GetStringVal(sFormat, "#")))))
    sCtrlNo = Replace(sCtrlNo, GetStringVal(sCtrlNo, "@"), sAlphabet)
    sCtrlNo = Replace(sCtrlNo, GetStringVal(sCtrlNo, "m"), Format(sDate, GetStringVal(sCtrlNo, "m")))
    sCtrlNo = Replace(sCtrlNo, GetStringVal(sCtrlNo, "d"), Format(sDate, GetStringVal(sCtrlNo, "d")))
    sCtrlNo = Replace(sCtrlNo, GetStringVal(sCtrlNo, "y"), Format(sDate, GetStringVal(sCtrlNo, "y")))
    Generate_CtrlNo2 = sCtrlNo
    oForm.FrmName.strNo = sCtrlNo
    'Set oRecordset = Nothing
    Set oForm = Nothing
End Function

Private Function GetStringVal(sVal As String, char As String) As String
Dim i As Integer
Dim sChar As String
    For i = 1 To Len(sVal)
        If char = Mid(sVal, i, 1) Then
            sChar = sChar & Mid(sVal, i, 1)
        End If
    Next i
    GetStringVal = sChar
End Function

Private Function FormatNumeric(n As Integer, nLen As Integer) As String
Dim i As Integer
Dim sVal As String
    For i = 1 To nLen
        sVal = sVal & "0"
    Next i
    FormatNumeric = sVal
End Function
'----------------------------------------------------------------------------------------------------------------------------------------------
Public Sub Update_CtrlNo2(sTranNo As String, sMenuName As String, sTranDate As String)
Dim sFormat As String
Dim sSQL As String, nPos As Integer, nVal As Integer
'Dim oDB As New clsDatabase
'Dim oRecordset As New clsRecordset
    sFormat = GetValueFrTable("cNumberFormat", "system_autonumber", "cCode ='" & sMenuName & "'")
    nPos = InStr(sFormat, GetStringVal(sFormat, "#"))
    nVal = CInt(Mid(sTranNo, nPos))
    
    sSQL = "UPDATE system_autonumber SET nCtr =" & nVal & ", cDate='" & sTranDate & "' WHERE cCode ='" & sMenuName & "' and cCompanyID = '" & Trim(COID) & "'"
    
    cn.Execute sSQL
    
    'Set oRecordset = Nothing
    'Set oDB = Nothing
End Sub


Public Function Check_CtrlNo2(rs As ADODB.Recordset, sTblName As String, _
sFldName As String, sTranNo As String, sMenuName As String, sDate As String) As String
'Dim oRecordset As New clsRecordset
Dim oForm As New clsFormSetup
Dim lCtrlNoOk As Boolean
Dim sFormat As String, sDateVal As String, sReset  As String, sAlphaVal  As String
Dim nNum As Integer
Dim strNewNo As String
On Error Resume Next
        strNewNo = sTranNo
        sFormat = GetValueFrTable("cNumberFormat", "system_autonumber", "cCode ='" & sMenuName & "'")
        sReset = GetValueFrTable("cResetCtr", "system_autonumber", "cCode ='" & sMenuName & "'")
        sAlphaVal = GetValueFrTable("cAlphaVal", "system_autonumber", "cCode ='" & sMenuName & "'")
        sDateVal = GetValueFrTable("cDate", "system_autonumber", "cCode ='" & sMenuName & "'")
        nNum = GetValueFrTable("nCtr", "system_autonumber", "cCode ='" & sMenuName & "'")
        If sDateVal = "" Then sDateVal = sDate
        Do Until lCtrlNoOk
            If IDExisting(rs, sFldName, sTblName, strNewNo) Then
                Select Case sReset
                    Case ""
                        nNum = nNum + 1
                    Case "NONE"
                        nNum = nNum + 1
                    Case "DAILY"
                        If Format(sDate, "MMDDYYYY") = Format(sDateVal, "MMDDYYYY") Then
                            nNum = nNum + 1
                        Else
                            nNum = 1
                        End If
                    Case "MONTHLY"
                        If Format(sDate, "MMYYYY") = Format(sDateVal, "MMYYYY") Then
                            nNum = nNum + 1
                        Else
                            nNum = 1
                        End If
                    Case "ANNUALY"
                        If Format(sDate, "YYYY") = Format(sDateVal, "YYYY") Then
                            nNum = nNum + 1
                        Else
                            nNum = 1
                        End If
                End Select
                strNewNo = Replace(sFormat, GetStringVal(sFormat, "#"), Format(nNum, FormatNumeric(nNum, Len(GetStringVal(sFormat, "#")))))
                strNewNo = Replace(strNewNo, GetStringVal(strNewNo, "@"), sAlphaVal)
                strNewNo = Replace(strNewNo, GetStringVal(strNewNo, "m"), Format(sDate, GetStringVal(strNewNo, "m")))
                strNewNo = Replace(strNewNo, GetStringVal(strNewNo, "d"), Format(sDate, GetStringVal(strNewNo, "d")))
                strNewNo = Replace(strNewNo, GetStringVal(strNewNo, "y"), Format(sDate, GetStringVal(strNewNo, "y")))
            Else
                lCtrlNoOk = True
            End If
        Loop
        
    Check_CtrlNo2 = strNewNo
    oForm.FrmName.strNo = strNewNo
    
    'Set oRecordset = Nothing
    Set oForm = Nothing
End Function

